package trb.complexshield.voxel;

import java.awt.Color;
import no.hrp.hvrc.utils.swing.UIUtils;

public class RaytraceApplication {
    public static void main(String[] args) {
        VoxelGrid voxelGrid = new VoxelGrid(16, 16, 16);
        populateCylinder(voxelGrid);
        voxelGrid.bitSet.set(0, voxelGrid.bitSet.size());
        Raytrace raytrace = new Raytrace(voxelGrid);
        Visualisation visualisation = new Visualisation(voxelGrid, 32);
        visualisation.clear(Color.WHITE);
        visualisation.paintVoxelGrid(Color.LIGHT_GRAY);
        visualisation.drawGrid(Color.BLACK);
        Line line = new Line(8, 14.5f, 8, 8, 1.5f, 8);
        visualisation.drawLine(line, Color.GREEN);
        raytrace.raytrace(line, visualisation);
        UIUtils.showImageInJFrame(visualisation.image);
    }

    public static void populateCylinder(VoxelGrid voxelGrid) {
        for (int zIdx=0; zIdx<voxelGrid.zdim; zIdx++) {
            for (int yIdx=0; yIdx<voxelGrid.ydim; yIdx++) {
                float y = yIdx - voxelGrid.ydim / 2 + 0.5f;
                for (int xIdx = 0; xIdx < voxelGrid.xdim; xIdx++) {
                    float x = xIdx - voxelGrid.xdim / 2 + 0.5f;
                    float length = (float) Math.sqrt(x * x + y * y);
                    voxelGrid.setBitAtIndex(xIdx, yIdx, zIdx, length < voxelGrid.xdim * 0.25f);
                }
            }
        }
    }

    public static void populateSphere(VoxelGrid voxelGrid) {
        for (int zIdx=0; zIdx<voxelGrid.zdim; zIdx++) {
            float z = (zIdx + 0.5f) / (voxelGrid.zdim - 1f) * 2 - 1;
            for (int yIdx=0; yIdx<voxelGrid.ydim; yIdx++) {
                float y = (yIdx + 0.5f) / (voxelGrid.ydim - 1f) * 2 - 1;
                for (int xIdx = 0; xIdx < voxelGrid.xdim; xIdx++) {
                    float x = (xIdx + 0.5f) / (voxelGrid.xdim - 1f) * 2 - 1;
                    float length = (float) Math.sqrt(x * x + y * y + z * z);
                    voxelGrid.setBitAtIndex(xIdx, yIdx, zIdx, length < 1);
                }
            }
        }
    }
}
